import { observable, action,} from 'mobx';
import * as Api from '../../api/create';
import { Form, SelectPage,BrowserHrm } from 'weaver-mobile-page'
import { RouteLayout } from 'weaver-mobile-page';
import {Tools,Bubble } from 'weaver-mobile';
const getLabel = Tools.getLabel;

export default class CreateStore {
  @observable pathString = '';
  @observable selectPage = new SelectPage.Store({route: '/calendar/create'});
  @observable repeatSelectPage = new SelectPage.Store({route: '/calendar/create'});
  @observable finishRepeat = new SelectPage.Store({route: '/calendar/create'});
  @observable createLayout = new RouteLayout.Store({route: '/calendar',id: 'create'})
  
  @observable browser = new BrowserHrm.Store({route: '/calendar/create'});
  @observable form = {};
  @observable layout = new RouteLayout.Store({route: '/calendar/create'})
  @observable repeatLayout = new RouteLayout.Store({route: '/calendar/create'})
  @observable endRepeatLayout = new RouteLayout.Store({route: '/calendar/create'})
  @observable idname = '';
  @observable currentTime ='';
  @observable startDateString = '';
  @observable endDateString = '';
  @observable timeBefore = '';
  @observable timeEnd = '';
  @observable isEdit = false;
  @observable workpalnID = '';
  @observable meetingID = ''; // 相关会议
  @observable repeatType = '0'; // 重复类型
  @observable isFinishRepeat = '0'; //结束重复
  @observable endrepeatDate = getLabel(389715,'永不');
  @observable remidOptions = []; // 提醒方式
  @observable selectDate = '';
  @observable createDisabled = false;
  @observable memberConflict = 0;
  @observable memberConflictChk = 0;
  @observable oldSecretLevel = '4';
  @observable oldMemberIds = '';
  @observable isOpenSecret = false;
  @observable fromType = '';

  @observable isfromchatshare = '';
  @observable sharer = '';
  @observable createInfo = {
    stringTimeBefore: '',
    stringTimeEnd: '',
  }
  @observable repeatInfo = {
    "isremind": 0, //是否提醒默认值
    "timeModul": '3', // 重复类型, 默认：3（按天）,0（按周）,1（按月）,2（按年）
    "interval": 1, // 天/周/月/年数
    "fer_0": [], // 每周周几默认值
    "fer_1": [], // 每月的几号
    "fer_2": [], // 每年的几月
    "fer_3": [1,1], // 第几个星期几[1,1]:第一个星期一
    "monthType": '0', // 每月日期正数:0,倒数: 1
    "yearType": '0', // 每年日期正数:0,倒数: 1
    "switchWeek": 0, //默认0: 日期，1:星期 
    "availableEndDate": '' //结束重复
  }
  @observable voiceCreateLoading = false;
  @observable detailDatas = {};
  @observable urlCreateParams = {};
  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this.createInfo[key] = params[key];
    });
    return this;
  }
  
  setOldSecretLevel = (val) => this.oldSecretLevel = val;

  setRepeatInfo = (params = {}) => {
    Object.keys(params).forEach(key => {
      this.repeatInfo[key] = params[key];
    });
    return this;
  }

  initRepeatInfo = () => {
    this.repeatInfo = {
      "isremind": 0, //是否提醒默认值
      "timeModul": '3', // 重复类型, 默认：3（按天）,0（按周）,1（按月）,2（按年）
      "interval": 1, // 天/周/月/年数
      "fer_0": [], // 每周周几默认值
      "fer_1": [], // 每月的几号
      "fer_2": [], // 每年的几月
      "fer_3": [1,1], // 第几个星期几[1,1]:第一个星期一
      "monthType": '0', // 每月日期正数:0,倒数: 1
      "yearType": '0', // 每年日期正数:0,倒数: 1
      "switchWeek": 0, //默认0: 日期，1:星期 
      "availableEndDate": '' //结束重复
    }
    this.endrepeatDate = getLabel(389715,'永不');
    this.remidOptions = [];
  }

  setParams = (params = {}, callback) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    typeof callback === 'function' && callback();
    return this;
  }

  // 新建日程基础数据
  getCreateWorkplanBaseData = (params,callback) => {
    this.workpalnID = '';
    let pathstr = window.location.hash && window.location.hash.split('#')[1].split('?');
    pathstr[1] = pathstr[0];
    this.form = new Form.Store({route: `${pathstr[1]}`})
    if(this.fromType != 'crm'){
      window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(18481,'新建日程') })
    }
    Api.getCreateWorkplanBaseData(params).then((res) => {
      this.detailDatas = res;
      res.msg&&Bubble.error(res.msg,2);
      this.oldSecretLevel='3';
      this.isOpenSecret = res.isOpenSecret;
      this.form.initForm(res.data,`${pathstr[1]}`);
      this.selectPage = new SelectPage.Store({route: `${pathstr[1]}`});
      this.repeatSelectPage = new SelectPage.Store({route: `${pathstr[1]}`});
      this.finishRepeat = new SelectPage.Store({route: `${pathstr[1]}`});
      // // this.createLayout = new RouteLayout.Store({route: `${pathstr[1]}`})
      this.browser = new BrowserHrm.Store({route: `${pathstr[1]}`});
      this.layout = new RouteLayout.Store({route: `${pathstr[1]}`});
      this.repeatLayout = new RouteLayout.Store({route: `${pathstr[1]}`});
      this.endRepeatLayout = new RouteLayout.Store({route: `${pathstr[1]}`})

      this.repeatType = '0';
      this.memberConflict = res.memberConflict
      this.memberConflictChk = res.memberConflictChk
      res.data && res.data[0].items.map(item => {
        if(item.domkey[0] === 'remindType') {
          this.remidOptions = item.value === '' ? [] : item.value.split(',');
        }
        if(item.domkey[0] === 'beginDateTime' || item.domkey[0] === 'endDateTime') {
          let time = item.value.split(' ');
          let timeStr = this.selectDate === '' ? item.value : this.selectDate + ' ' + time[1];
          this.form.updateField(item.domkey[0], timeStr);
        }
        if(item.domkey && item.domkey[0] == 'memberIDs'){
          this.oldMemberIds = item.value;
        }
        return '';
      })
      if (callback) callback(true)
    }); 
  }

  // 编辑日程
  getEditWorkplanBaseData = (params,callback) => {
    let param = {...params,workid: this.workpalnID,isEdit: '1'};
    if(this.fromType != 'crm'){
      window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(384903,'编辑日程') })
    }
    Api.viewWorkplanBaseData(param).then((res) => {
      // 在不同url下编辑日程时,动态改变route
      this.detailDatas = res;
      let pathstr = window.location.hash && window.location.hash.split('#');
      this.oldSecretLevel=res.secretLevel;
      this.isOpenSecret = res.isOpenSecret;
      this.form = new Form.Store({route: `${pathstr[1]}`})
      this.selectPage = new SelectPage.Store({route: `${pathstr[1]}`});
      this.repeatSelectPage = new SelectPage.Store({route: `${pathstr[1]}`});
      this.finishRepeat = new SelectPage.Store({route: `${pathstr[1]}`});
      // this.createLayout = new RouteLayout.Store({route: `${pathstr[1]}`})
      this.browser = new BrowserHrm.Store({route: `${pathstr[1]}`});
      this.layout = new RouteLayout.Store({route: `${pathstr[1]}`});
      this.repeatLayout = new RouteLayout.Store({route: `${pathstr[1]}`});
      this.endRepeatLayout = new RouteLayout.Store({route: `${pathstr[1]}`})
      this.memberConflict = res.memberConflict
      this.memberConflictChk = res.memberConflictChk
      this.form.initForm(res.data,`${pathstr[1]}`);
      if(this.form && this.form.formParams && this.form.formParams.planName) {// 过滤br
        let br = new RegExp(/&lt;br&gt;/g);
        let name = this.form.formParams.planName.replace(br, ' ');
        this.form.updateField('planName', name);
      }
      res.data && res.data[0].items.map(item => {
        if(item.domkey[0] === 'remindType') {
          this.remidOptions = item.value === '' ? [] : item.value.split(',');
          this.form.updateFields({remindType:{value:item.value}})
        }
        if(item.domkey[0] === 'repeatType') {
          this.setRepeatInfo({...item.datas}); // 编辑周期日程时初始化表单
          this.repeatType = item.value; // 重复类型
        }
        if(item.domkey[0] === 'moudulEndDate') {
          this.endrepeatDate = item.availableEndDate;
          this.setRepeatInfo({ availableEndDate: item.availableEndDate });
        }
        if(item.domkey[0] === 'beginDateTime') {
          this.startDateString = item.value;
        }
        if(item.domkey[0] === 'endDateTime') {
          this.endDateString = item.value;
        }
        if(item.domkey && item.domkey[0] == 'memberIDs'){
          this.oldMemberIds = item.value;
        }
        return '';
      });
      if (callback) callback(true)
    }); 
  }

  // 语音创建日程
  addByWord = (text,callback,datas) => {
    let param = {text: text, ismobile: 1};
    if (datas) {
      param = datas;
    }
    Api.addByWord(param).then(res => {
      if(res.api_status) {
        if(typeof(callback) === 'function') {
          callback()
          Bubble.success(getLabel(18758,'保存成功'),1);
        }
      }else{
        Bubble.error(getLabel(22620,'保存失败'),1);
      }
    }, (res) => {

    });
  }

  groupToggle = (...arg) => {
    let title = arg[0];
    let status = arg[1];
    this.form.conditioninfo = this.form.conditioninfo.map(res => ({
      ...res,
      ...(res.title === title ? {show: status} : {})
    }));
  }
 
}
